package com.luo.chengrui.module.sa.api.org;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.google.gson.Gson;
import com.luo.chengrui.module.sa.api.org.dto.OrgRespDTO;
import com.luo.chengrui.module.sa.controller.admin.matrix.vo.BusinessManagerMatrixValue;
import com.luo.chengrui.module.sa.controller.admin.matrix.vo.OporgManagerMatrixConfigDataValue;
import com.luo.chengrui.module.sa.controller.admin.matrix.vo.OporgManagerMatrixConfigReqVO;
import com.luo.chengrui.module.sa.controller.admin.matrix.vo.OporgManagerMatrixPersonRespVO;
import com.luo.chengrui.module.sa.convert.org.OrgConvert;
import com.luo.chengrui.module.sa.dal.dataobject.matrix.BusinessManagerMatrixDO;
import com.luo.chengrui.module.sa.dal.dataobject.org.OrgDO;
import com.luo.chengrui.module.sa.service.matrix.BusinessManagerMatrixService;
import com.luo.chengrui.module.sa.service.matrix.OporgManagerMatrixConfigService;
import com.luo.chengrui.module.sa.service.org.OrgService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class SaOrgMatrixApiImpl implements SaOrgMatrixApi {
    @Resource
    private OrgService orgService;

    @Resource
    private OporgManagerMatrixConfigService oporgManagerMatrixConfigService;
    @Resource
    private BusinessManagerMatrixService businessManagerMatrixService;

    @Override
    public CommonResult<List<OrgRespDTO>> getOrgManagerMatrixPerson(String orgId, String orgManagerDefineId) {
        OporgManagerMatrixConfigReqVO vo = new OporgManagerMatrixConfigReqVO();
        vo.setOrgId(orgId);
        vo.setManagerDefineId(orgManagerDefineId);
        OporgManagerMatrixPersonRespVO respVO = oporgManagerMatrixConfigService.getOrgManagerMatrixPerson(vo);
        if (respVO != null) {
            return CommonResult.success(OrgConvert.INSTANCE.convertList3(respVO.getPersonList()));
        }
        return CommonResult.success(new ArrayList<>());
    }

    @Override
    public CommonResult<List<OrgRespDTO>> getBizManagerMatrixPerson(String id) {
        BusinessManagerMatrixDO businessManagerMatrixDO = businessManagerMatrixService.getBusinessManagerMatrix(id);

        String dataValues = businessManagerMatrixDO.getDataValues();
        if (StrUtil.isNotEmpty(dataValues)) {
            JSONArray jsonArray = new JSONArray(dataValues);
            Gson gson = new Gson();
            List<BusinessManagerMatrixValue> dataValueList = jsonArray.stream().map(o -> gson.fromJson(o.toString(), BusinessManagerMatrixValue.class)).collect(Collectors.toList());

            List<String> personFullIds = dataValueList.stream().map(o -> o.getOrgFullId()).collect(Collectors.toList());
            List<OrgDO> personList = orgService.getPsmListByFullIds(personFullIds);
            return CommonResult.success(OrgConvert.INSTANCE.convertList3(personList));
        }
        return CommonResult.success(new ArrayList<>());
    }
}
